﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for UseBusiness
/// </summary>
public class UserBusiness
{
	public UserBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool Insert(UserEntities item)
    {
        bool flat = false;
        string[] para = { "@userName", "@password", "@fullName", "@birthday", "@gender", "@email",
                            "@address", "@phoneNumber", "@creditCard", "@cardNumber", "@reward", "@roleID" };
        string[] values = { item._userName,item._pass,item._fullName,item._birthday.ToString(),item._gender.ToString(),item._email,
                              item._address,item._phoneNumber.ToString(),item._creaitCard,item._cardNumber,item._reward.ToString(),item._roleID.ToString() };
        if (DataAccess.getExecProcedure("sp_InsertUser", para, values) > 0)
        {
            flat = true;
        }
        else
            flat = false;
        return flat;
    }
    public bool Insert(string[] values)
    {
        bool flat = false;
        string[] para = { "@userName", "@password", "@fullName", "@birthday", "@gender", "@email",
                            "@address", "@phoneNumber", "@creditCard", "@cardNumber", "@reward", "@roleID" };
        
        if (DataAccess.getExecProcedure("sp_InsertUser", para, values) > 0)
        {
            flat = true;
        }
        else
            flat = false;
        return flat;
    }

    public bool Edit(UserEntities item)
    {
        bool flat = false;
        string[] para = { "@userID", "@userName", "@password", "@fullName", "@birthday", "@gender", "@email",
                            "@address", "@phoneNumber", "@creditCard", "@cardNumber", "@reward", "@roleID" };
        string[] values = { item._userID.ToString(),item._userName,item._pass,item._fullName,item._birthday.ToString(),item._gender.ToString(),item._email,
                              item._address,item._phoneNumber.ToString(),item._creaitCard,item._cardNumber,item._reward.ToString(),item._roleID.ToString() };
        if (DataAccess.getExecProcedure("sp_UpdateUser", para, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Edit(string[] values)
    {
        bool flat = false;
        string[] para = { "@userID", "@userName", "@password", "@fullName", "@birthday", "@gender", "@email",
                            "@address", "@phoneNumber", "@creditCard", "@cardNumber", "@reward", "@roleID" };
        
        if (DataAccess.getExecProcedure("sp_UpdateUser", para, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Delete(int id)
    {
        if (DataAccess.getExec("exec sp_DeleteUser '"+id+"'") > 0)
            return true;
        else
            return false;
    }


    public DataTable getAllUser()
    {
        return DataAccess.DataTable_Query("exec sp_GetUser '0'");
    }
    public UserEntities getUser(int id)
    {
        DataTable dt= DataAccess.DataTable_Query("exec sp_GetUser '" +id.ToString() +"'");
        UserEntities ue = new UserEntities();
        if (dt != null)
        {
            DataRow dr = dt.Rows[0];
            ue._userID = int.Parse(dr["userID"].ToString());
            ue._userName = dr["userName"].ToString();
            ue._pass = dr["userpassword"].ToString();
            ue._fullName = dr["fullName"].ToString();
            ue._birthday = dr["birthday"].ToString();
            if (dr["gender"].ToString() == "true")
                ue._gender = "Nam";
            else
                ue._gender = "Nu";
            ue._email = dr["email"].ToString();
            ue._address = dr["useraddress"].ToString();
            ue._phoneNumber = int.Parse(dr["phoneNumber"].ToString());
            ue._creaitCard = dr["creditCard"].ToString();
            ue._cardNumber = dr["cardNumber"].ToString();
            ue._reward = int.Parse(dr["reward"].ToString());
            ue._roleID = dr["roleID"].ToString();
            ue._roleID = dr["roleID"].ToString();
        }
        return ue;
    }

    public int countUser(int roleID)
    {
        return (int)DataAccess.getScalar("exec sp_CountUser '" + roleID + "'");
    }

    public UserEntities login(string userName, string password)
    {
        UserEntities ue = new UserEntities();
        SqlDataReader dr = DataAccess.DataReader_getReader("exec sp_GetUserLogin '"+userName+"','" +password+"'");
        if (dr.Read())
        {
            ue = new UserEntities();
            ue._userID = int.Parse(dr["userID"].ToString());
            ue._userName = dr["userName"].ToString();
            ue._pass = dr["userpassword"].ToString();
            ue._fullName = dr["fullName"].ToString();
            ue._birthday = dr["birthday"].ToString();
            if (dr["gender"].ToString() == "true")
                ue._gender = "Nam";
            else
                ue._gender = "Nu";
            ue._email = dr["email"].ToString();
            ue._address = dr["useraddress"].ToString();
            ue._phoneNumber = int.Parse(dr["phoneNumber"].ToString());
            ue._creaitCard = dr["creditCard"].ToString();
            ue._cardNumber = dr["cardNumber"].ToString();
            ue._reward = int.Parse(dr["reward"].ToString());
            ue._roleID = dr["roleID"].ToString();

        }
        else
            return null;
        return ue;
    }
    public bool checkUser(string userName)
    {
        bool flat = false;
        SqlDataReader dr = DataAccess.DataReader_getReader("exec sp_GetUserName '" + userName + "'");
        if (dr.Read())
            flat = false;
        else
            flat = true;          
        
        return flat;
    }
}